"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.app = void 0;
const node_sdk_1 = __importDefault(require("@cloudbase/node-sdk"));
const util_1 = require("./util");
const config_1 = __importDefault(require("./config"));
exports.app = node_sdk_1.default.init(config_1.default.cloudConfig);
const db = exports.app.database();
const _ = db.command;
// lookup 连表  https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/aggregate/Aggregate.lookup.html#%E7%A4%BA%E4%BE%8B
const statusMenu = {
    err: 400,
};
const DbUtil = {
    db,
    _,
    async colectionList() {
        // const res = await db.serverDate;
        // console.log(res);
    },
    async createCollection(collection) {
        const res = await db.createCollection(collection);
        return res;
    },
    async query(collection, options = {}) {
        //field 表示要返回的字段
        let { where = {}, field = {}, needTotal = true } = options;
        let conn = await db.collection(collection);
        if (typeof where === "string") {
            // 处理传递的参数有特殊字符
            where = eval(`(${where})`);
        }
        let { pageIndex = 1, pageSize = 10 } = where, others = __rest(where, ["pageIndex", "pageSize"]);
        delete others.collectoin;
        conn = await conn.where(others);
        conn = await conn
            .skip((Number(pageIndex) - 1) * pageSize)
            .limit(Number(pageSize));
        const res = await conn.field(field).get();
        let count = 0;
        if (needTotal) {
            count =
                (await (await db.collection(collection).where(others).count()).total) ||
                    0;
        }
        const respon = util_1.dealDbMessage(res.data, 0, "", {
            pageInfo: {
                total: count,
            },
        });
        return respon;
    },
    async insert(collection, data = {}) {
        const res = await db.collection(collection).add(data);
        if (res.id) {
            return util_1.dealDbMessage(res);
        }
        else {
            return util_1.dealDbMessage("", statusMenu.err, "插入失败");
        }
    },
    async update(collection, id, data = {}) {
        try {
            if (data._id) {
                delete data._id;
                delete data.id;
            }
        }
        catch (err) {
            console.log(err);
        }
        const res = await db.collection(collection).doc(id).update(data);
        if (res.requestId && res.updated && res.updated > -1) {
            return util_1.dealDbMessage(res.updated);
        }
        else {
            return util_1.dealDbMessage(res.updated, statusMenu.err);
        }
    },
    async delete(collection, id) {
        const res = await db.collection(collection).doc(id).remove();
        if (res.deleted > -1) {
            return util_1.dealDbMessage(res.deleted);
        }
        else {
            return util_1.dealDbMessage(res.deleted, statusMenu.err, "没有找到该数据");
        }
    },
};
exports.default = DbUtil;
